/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import javax.swing.*;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

/**
 * This class provides the paint methods for the layout of main tabs of
 * Pear Review.
 * 
 * @author Dimitrij Pankratz, Anton Musichin
 * @version 1.0
 *
 */
public class MainTabsUI extends BasicTabbedPaneUI {
	/**
	 * Insets of the tabs.
	 */
	protected static final Insets TAB_INSETS = new Insets(1, 0, 0, 0);
	
	@Override
	protected void installDefaults() {
		//super.installDefaults();
		
		this.tabInsets = new Insets(0,0,0,0);
		this.selectedTabPadInsets = new Insets(0,0,0,0);
		this.tabAreaInsets  = new Insets(0,0,0,0);
	}
	
	@Override
	protected Insets getContentBorderInsets(int tabPlacement) {
		return TAB_INSETS;
	}

	@Override
	protected int calculateTabWidth(int tabPlacement, int tabIndex, 
			FontMetrics metrics) {
		return super.calculateTabWidth(tabPlacement, tabIndex, metrics) + 
			metrics.getHeight();
	}
	
	@Override
	public void paint(Graphics g, JComponent c) {
		super.paint(g, c);
		
		Color gradientTop = new Color(0x9a, 0x9a, 0x9a);
		Color gradientBottom = new Color(0x86, 0x86, 0x86);
		
		Color borderColor = new Color(0x66, 0x66, 0x66);
		Stroke borderStroke = new BasicStroke(1.0f);
		
		Graphics2D g2d = (Graphics2D) g;
		
		// Enable anti-aliasing
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		int x = 0;
		int y = 0;
		int w = c.getWidth();
		int h = calculateMaxTabHeight(1);
		int cornerRound = 0;
		
		g2d.setColor(Color.BLACK);
		g2d.fillRect(x, y, w, h);

		Paint paint = new GradientPaint(0, y, gradientTop, 0, y + h, 
				gradientBottom);
		
		g2d.setPaint(paint);
		g2d.fillRoundRect(x, y, w, h, (int) cornerRound, (int) cornerRound);
		
		Shape tabShape2 = new RoundRectangle2D.Double((double) x, (double) y,
				(double) w, (double) h, cornerRound, cornerRound);

		g2d.setColor(borderColor);
		g2d.setStroke(borderStroke);
		g2d.draw(tabShape2);
	}
	
	@Override
	protected void paintTabBackground(Graphics g, int tabPlacement, 
			int tabIndex, int x, int y, int w, int h, boolean isSelected ) {
		
		Graphics2D g2d = (Graphics2D) g;
		int cornerRound = 0;
		Color gradientTop;
		Color gradientBottom;
		Color borderColor = new Color(0x66, 0x66, 0x66);
		Stroke borderStroke = new BasicStroke(1.0f);
		
		if (isSelected) {
			gradientTop = new Color(0xf1, 0xf1, 0xf1);
			gradientBottom = new Color(0xd8, 0xd8, 0xd8);
		} else { 
			gradientTop = new Color(0x9a, 0x9a, 0x9a);
			gradientBottom = new Color(0x86, 0x86, 0x86);
		}
		
		// Enable anti-aliasing
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		g2d.setColor(Color.BLACK);
		g2d.fillRect(x, y, w, h);
		
		Paint paint = new GradientPaint(0, y, gradientTop, 0, y + h, 
				gradientBottom);
			
		g2d.setPaint(paint);
		g2d.fillRoundRect(x, y, w, h, (int) cornerRound, (int) cornerRound);
			
		Shape tabShape2 = new RoundRectangle2D.Double((double) x, (double) y,
				(double) w, (double) h, cornerRound, cornerRound);

		g2d.setColor(borderColor);
		g2d.setStroke(borderStroke);
		g2d.draw(tabShape2);
	}
	
	@Override
	protected void paintTab(Graphics g, int tabPlacement,
			Rectangle[] rects, int tabIndex, 
			Rectangle iconRect, Rectangle textRect) {
		
		Rectangle tabRect = rects[tabIndex];
		int selectedIndex = tabPane.getSelectedIndex();
		boolean isSelected = selectedIndex == tabIndex;

		paintTabBackground(g, tabPlacement, tabIndex, tabRect.x, tabRect.y,
				tabRect.width, tabRect.height, isSelected);	
	}
	
	@Override
	protected int getTabLabelShiftY(int tabPlacement, int tabIndex, 
			boolean isSelected) {
		return -1;
	}
	
	@Override
	protected JButton createScrollButton(int direction) {
		return new ScrollJButton(direction);
	}
	
	protected class ScrollJButton extends BasicArrowButton 
			implements UIResource {
		private static final long serialVersionUID = 9194416893236333917L;

		ScrollJButton(int direction) {
			super(direction);
			setBorder(null);
		}
		
		@Override
		public void paint(Graphics g) {
			Color gradientTop = new Color(0x9a, 0x9a, 0x9a);
			Color gradientBottom = new Color(0x86, 0x86, 0x86);
			
			Graphics2D g2d = (Graphics2D) g;
			
			// Enable anti-aliasing
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

			Paint paint = new GradientPaint(0, 
					0, 
					gradientTop, 
					0, 
					getHeight(), 
					gradientBottom);
			g2d.setPaint(paint);
			g2d.fillRect(0, 0, getWidth(), getHeight());
			
			int size = Math.min(getWidth(), getHeight()) / 4;
			if(size % 2 == 1) {
				size++;
			}
			int x = (getHeight() - size ) / 2;
			int y = (getHeight() - size ) / 2;
			paintTriangle(g, 
					x, 
					y, 
					size, 
					getDirection(), 
					isEnabled());
		}
		
		@Override
		public Dimension getPreferredSize() {
			return new Dimension(MainTabsUI.this.maxTabHeight - 1, 
					MainTabsUI.this.maxTabHeight - 1);
		}
		
		@Override
		public Dimension getMaximumSize() {
			return getPreferredSize();
		}
		
		@Override
		public Dimension getMinimumSize() {
			return getPreferredSize();
		}
	}
}